//
//  JFTabBar.m
//  oc1
//
//  Created by kokuma on 2017/2/12.
//  Copyright © 2017年 kokumams. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "JFTabBar.h"

@interface JFTabBar ()

@end

@implementation JFTabBar

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        //
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.shadowImage = [[UIImage alloc]init];
        [self addSubview: self.addButton];
    }
    return self;
}

- (void)didTappedAddButton{
    //
}

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat screen = [UIScreen mainScreen].bounds.size.width;
    CGFloat y=0;
    CGFloat width=screen/5;
    CGFloat height=49;
    CGFloat index=0;
    for(UIView* view in self.subviews) {
        if (![view isKindOfClass: NSClassFromString(@"UITabBarButton")]) {
            // 隐藏tabBar顶部横线
            if ([view isKindOfClass:NSClassFromString(@"UIImageView")]&&view.bounds.size.height <= 1) {
              //  view.hidden = true;
            }
            continue;
        }
        UIControl* btn =  view;
        [btn addTarget:self action: @selector(anim:)  forControlEvents: UIControlEventTouchUpInside];
        CGFloat x = (index > 1 ? index + 1 : index) * width;
        view.frame = CGRectMake(x,y,width,height);
        index += 1;
    }
}

- (void)anim: (UIControl*) btn{
   // [self aniDamping:btn];
}

//弹簧动画
- (void)aniDamping:(UIView*)view{
    view.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:1 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0  animations:^{
        view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        //
    }];
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    return [super hitTest:point withEvent: event];
}

- (UIButton *) addButton{
    CGFloat SCREEN_WIDTH = [UIScreen mainScreen].bounds.size.width;
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.5,49 * 0.5 - 8,SCREEN_WIDTH / 5,SCREEN_WIDTH / 5)];
    //CGPointMake(SCREEN_WIDTH * 0.5,49 * 0.5 - 8);
    CGPoint point={SCREEN_WIDTH * 0.5,49 * 0.5 - 8};
    addButton.center= point;
    [addButton addTarget:self action: @selector(didTappedAddButton) forControlEvents: UIControlEventTouchUpInside];
    [addButton setImage:[UIImage imageNamed: @"tab_publish_n"] forState: UIControlStateNormal];
    [addButton setImage:[UIImage imageNamed: @"tab_publish_h"] forState: UIControlStateHighlighted];
    return addButton;
}

@end